package com.ourhours.message.service.impl;

import cn.jpush.api.push.PushResult;
import com.ourhours.message.domain.AppPush;
import com.ourhours.message.domain.Supplier;
import com.ourhours.message.dto.PushResponse;
import com.ourhours.message.enums.MessageTypeEnum;
import com.ourhours.message.enums.SupplierTypeEnum;
import com.ourhours.message.exception.MessageException;
import com.ourhours.message.mapper.AppPushMapper;
import com.ourhours.message.rpc.reference.jiguang.JiGuangRpc;
import com.ourhours.message.service.AppPushService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ourhours.message.service.wrapper.SupplierWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 消息发送记录表 服务实现类
 * </p>
 *
 * @author dalong.wang
 * @since 2018-03-21
 */
@Service
public class AppPushServiceImpl extends ServiceImpl<AppPushMapper, AppPush> implements AppPushService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppPushServiceImpl.class);

    @Autowired
    SupplierWrapper supplierWrapper;

    @Autowired
    JiGuangRpc jiGuangRpc;

    @Autowired
    AppPushService appPushService;

    @Override
    public PushResponse pushing(Long vendorId, String userId, String title, String content, String action){

        Supplier supplier = supplierWrapper.findSupplierWithCache(vendorId, MessageTypeEnum.APP_PUSH.getCode());

        if(supplier==null){
            LOGGER.error("AppPushServiceImpl.pushing获取供应商信息失败,参数:{},结果:{}",vendorId,supplier);
            throw new MessageException("根据vendorId查询推送供应商表数据为空！");
        }
        PushResponse pushResponse = new PushResponse();
        switch (SupplierTypeEnum.fromCode(supplier.getSupplierType())){
            case APP_PUSH_JPUSH :
                //调用极光
                PushResult result = jiGuangRpc.pushing(supplier.getOuterAppKey(),supplier.getOuterSecret(),userId,title,content,action);

                if(result!=null && result.getResponseCode()==200){
                    pushResponse.setCode(200);
                    pushResponse.setFailureReason(result.getOriginalContent());
                    LOGGER.error("AppPushServiceImpl.pushing发送极光推送成功，参数:{},{},{},{},{},结果:{}", vendorId, userId, title, content,action,pushResponse);
                }else{
                    pushResponse.setCode(404);
                    pushResponse.setFailureReason("调用极光异常");
                    LOGGER.error("AppPushServiceImpl.pushing发送极光推送失败，参数:{},{},{},{},{},结果:{}", vendorId, userId, title, content,action,pushResponse);
                }
                break;
            default:
        }

        return pushResponse;


    }

    @Override
    public void setAppPushStatus(Long id, Integer status, String errCode, String reason) {
        try {
            AppPush entity = new AppPush();
            entity.setId(id);
            entity.setStatus(status);
            if (status == -1) {
                entity.setFailureReason(errCode);
                entity.setFailureReason(reason);
            }
            entity.setUpdateTime(new Date());
            entity.setUpdateUser("MQ");
            this.updateById(entity);
        } catch (Exception e) {
            LOGGER.error("保存记录状态失败，但不处理异常，参数:{},{},{},{},原因:{}", id, status, errCode, reason, e);
        }
    }

}
